﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace The_Wumpers
{
	class Cave
	{
		int[] readIntsArray = new int[90];
        Room[] roomArray;

		public Cave() { 
            fileReader();
            createRoomArray();
        }
		public void fileReader()
		{
            Random rnd = new Random();
            int temprand = rnd.Next(0,3);
            string text = "";
            if (temprand == 0){ text = System.IO.File.ReadAllText("CaveFile1.txt"); }
            if (temprand == 1){ text = System.IO.File.ReadAllText("CaveFile1.txt"); }
            if (temprand == 2){ text = System.IO.File.ReadAllText("CaveFile2.txt"); }

			Console.WriteLine(text);
			
			string[] words = text.Split(',');
			int index = 0;
			foreach (string word in words)
			{
				try
				{
					int temp = Convert.ToInt32(word);
					readIntsArray[index] = temp;
					Console.WriteLine(temp);
				}catch{}
				index++;
			}
			
		}
		private void createRoomArray()
		{
			roomArray = new Room[30];

            for (int i = 0; i < 30; i++)
            {
                roomArray[i] = new Room();
                if (this.readIntsArray[i * 3] == 0)
                    this.roomArray[i].NorthWestOpen = true;
                else this.roomArray[i].NorthWestOpen = false;

                if (this.readIntsArray[(i * 3) + 1] == 0)
                    this.roomArray[i].NorthOpen = true;
                else this.roomArray[i].NorthOpen = false;

                if (this.readIntsArray[(i * 3) + 2] == 0)
                    this.roomArray[i].NorthEastOpen = true;
            }
             
            for(int i = 0; i <30; i++)
            {
                //test for sw 
                if (i >= 24) { roomArray[i].SouthWestOpen = roomArray[(i + 5) % 6].NorthEastOpen; roomArray[i].SouthWest = (i + 5) % 6; }
                else if (i % 6 == 0) { roomArray[i].SouthWestOpen = roomArray[i + 11].NorthEastOpen; roomArray[i].SouthWest = i + 11; }
                else { roomArray[i].SouthWestOpen = roomArray[i + 5].NorthEastOpen; roomArray[i].SouthWest = i + 5; }

                //Test For se
                if (i >= 24) { roomArray[i].SouthEastOpen = roomArray[(i + 1) % 6].NorthWestOpen; roomArray[i].SouthEast = (i + 1) % 6; }
                else if ((i + 1) % 6 == 0) { roomArray[i].SouthEastOpen = roomArray[i + 1].NorthWestOpen; roomArray[i].SouthEast = i + 1; }
                else { roomArray[i].SouthEastOpen = roomArray[i + 7].NorthWestOpen; roomArray[i].SouthEast = i + 7; }

                //Test for south
                if (i >= 24) { roomArray[i].SouthOpen = roomArray[i - 24].NorthOpen; roomArray[i].South = i - 24; }
                else { roomArray[i].SouthOpen = roomArray[i + 6].NorthOpen; roomArray[i].South = i + 6; }
               
                //find second half of doors
                //Set The northern borders
                //NW
                if (i == 0) roomArray[i].northWest = 29;
                else if (i <= 5) roomArray[i].northWest = 23 + i;
                else if (i % 6 == 0) roomArray[i].northWest = i - 1;
                else roomArray[i].northWest = i - 7;
                //North
                if (i <= 5) roomArray[i].north = 24 + i;
                else { roomArray[i].north = i - 6; }
                //NE
                if (i == 5) roomArray[i].northEast = 24;
                else if (i <= 5) roomArray[i].northEast = 25 + i;
                else if ((i + 1) % 6 == 0) roomArray[i].northEast = i - 11;
                else roomArray[i].northEast = i - 5;

                roomArray[i].PrintOutStats(i);
            }
            //Set to global variable
            roomArray = roomArray;
            
		}
        public Room[] RoomArray
        {
            get {return roomArray;}

            set { roomArray = value; }
        }
	}
}
